﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace odinshrine
{
    class Collisions 
    {

        public static Color[,] TextureColor(SpriteManager tex)
        {
            Color[] color = new Color[tex.texture.Width * tex.texture.Height];
            Color[,] color2d = new Color[tex.width, tex.height];

            tex.texture.GetData(color);

            for (int y = (tex.FrameHauteur - 1) * (tex.height); y < tex.FrameHauteur  * tex.height ; y++)
            {
                for (int x = tex.FrameIndex * tex.width; x < (tex.FrameIndex + 1) * tex.width; x++)
                {
                    color2d[x - (tex.FrameIndex * tex.width), y - ((tex.FrameHauteur - 1 ) * tex.height)] = color[x + (y * tex.texture.Width)];
                }
            }

            return color2d;
        }

        public static bool Rectangles(Rectangle objet1, Rectangle objet2)
        {
            return (Rectangle.Intersect(objet1, objet2) != Rectangle.Empty);
        }

        public static bool Pixel(Color[,] color1, Color[,] color2, Vector2 pos1, Vector2 pos2)
        {
            int width1 = color1.GetLength(0), width2 = color2.GetLength(0), height1 = color1.GetLength(1), height2 = color2.GetLength(1);
            int top, bottom, left, right;

            top = Math.Max((int)pos1.Y, (int)pos2.Y);
            bottom = Math.Min((int)pos1.Y + (int)height1, (int)pos2.Y + (int)height2);
            left = Math.Max((int)pos1.X, (int)pos2.X);
            right = Math.Min((int)pos1.X + (int)width1, (int)pos2.X + (int)width2);

            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    if (color1[(int)x - (int)pos1.X, (int)y - (int)pos1.Y].A > 250 && color2[(int)x - (int)pos2.X, (int)y - (int)pos2.Y].A > 250)
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool Ecran(Rectangle objet1, GraphicsDeviceManager graphics)
        {
            if (objet1.X < 0 || (objet1.X + objet1.Width) > graphics.PreferredBackBufferWidth)
            {
                return true;
            }
            else if ((objet1.Y + objet1.Height) > graphics.PreferredBackBufferHeight)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool General(SpriteManager objet1, SpriteManager objet2)
        {
            if (Rectangles(new Rectangle((int)objet1.Position.X, (int)objet1.Position.Y, (int)objet1.width, (int)objet1.height), new Rectangle((int)objet2.Position.X, (int)objet2.Position.Y, (int)objet2.width, (int)objet2.height)))
            {
                Color[,] tobjet1 = TextureColor(objet1), tobjet2 = TextureColor(objet2);
                if ( Pixel(tobjet1,tobjet2, objet1.Position,objet2.Position))
                {
                    return (true);
                }
            }
            return (false);
        }
    }
}
